import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../apiService/user.dart';
import '../../components/functionButton.dart';
import '../Login/loginPage.dart';

class MyController extends GetxController {
  var userInfo = UserInfo().obs;

  @override
  void onInit() {
    super.onInit();
    getUser();
  }

  Future<void> getUser() async {
    EasyLoading.show(status: '加载中...', maskType: EasyLoadingMaskType.black);
    try {
      final response = await UserApi().getUserInfo();
      if (response != null) {
        userInfo.value = response;
      }
    } catch (e) {
      EasyLoading.showError('加载失败');
    } finally {
      EasyLoading.dismiss();
    }
  }

  Future<void> logout() async {
    Get.offAll(LoginPage());
    //清除shared_preference的数据
    final prefs = await SharedPreferences.getInstance();
    await prefs.clear();
  }
}


class MyPage extends StatelessWidget {
  final MyController controller = Get.put(MyController());

  MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return  SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(16),
          child: Column(
            children: [
              Row(
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(8),
                        boxShadow: [
                          BoxShadow(
                            color: Color.fromRGBO(149, 157, 165, 0.2),
                            // rgba(149, 157, 165, 0.2)
                            offset: Offset(0, 8),
                            // (0px, 8px)
                            blurRadius: 24,
                            // 24px
                            spreadRadius: 0, // 0px
                          ),
                        ],
                      ),
                      child: Obx(
                        () => Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              '用户名 ${controller.userInfo.value.name}',
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Text(
                              '邮箱 ${controller.userInfo.value.email}',
                              style: TextStyle(fontSize: 16),
                            ),
                            Text(
                              '电话 ${controller.userInfo.value.phone}',
                              style: TextStyle(fontSize: 16),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 16),
              Row(
                children: [
                  Expanded(
                    child: FunctionButton(
                      onPressed: controller.logout,
                      title: '退出登录',
                      color: Colors.red,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
    );
  }
}
